<template>
  <div class="m-2.5 p-2.5">
    <!--<el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="86px"
    >
       <el-form-item label="目录id" prop="cateId">
        <el-input
          v-model="queryParams.cateId"
          placeholder="请输入目录id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据源id" prop="dsId">
        <el-input
          v-model="queryParams.dsId"
          placeholder="请输入数据源id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据表id" prop="tbId">
        <el-input
          v-model="queryParams.tbId"
          placeholder="请输入数据表id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> 
      <el-form-item label="所属单位" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入所属单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据列名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入数据列名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据列注释" prop="comment">
        <el-input
          v-model="queryParams.comment"
          placeholder="请输入数据列注释"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否主键" prop="pk">
        <el-select v-model="queryParams.pk" @change="handleQuery" placeholder="请选择">
          <el-option label="是" :value="true" />
          <el-option label="否" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="允许null" prop="ableBeNull">
        <el-select v-model="queryParams.ableBeNull" @change="handleQuery" placeholder="请选择">
          <el-option label="是" :value="true" />
          <el-option label="否" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="默认展示" prop="defaultShow">
        <el-select v-model="queryParams.defaultShow" @change="handleQuery" placeholder="请选择">
          <el-option label="是" :value="true" />
          <el-option label="否" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="脱敏器" prop="desensitizer">
        <el-input
          v-model="queryParams.desensitizer"
          placeholder="请输入脱敏器"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input
          v-model="queryParams.length"
          placeholder="请输入长度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小数" prop="decimalLength">
        <el-input
          v-model="queryParams.decimalLength"
          placeholder="请输入小数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="默认值" prop="defaultValue">
        <el-input
          v-model="queryParams.defaultValue"
          placeholder="请输入默认值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序字段" prop="sortValue">
        <el-input
          v-model="queryParams.sortValue"
          placeholder="请输入排序字段"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->

    <div class="flex justify-between gap-1.5 mb-3">
      <div>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['web:data:query:sdkDqDsTbCl:add']"
          >
            新增
          </el-button>
        </el-col>
      </div>
      <div>
        <el-button
          type="primary"
          plain
          @click="handleSave"
          v-hasPermi="['web:data:query:sdkDqDsTbCl:add']"
        >
          保存
        </el-button>
      </div>
    </div>
    <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['web:data:query:sdkDqDsTbCl:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate({})"
          v-hasPermi="['web:data:query:sdkDqDsTbCl:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete({})"
          v-hasPermi="['web:data:query:sdkDqDsTbCl:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Upload"
          @click="handleImport"
          v-hasPermi="['web:data:query:sdkDqDsTbCl:import']"
        >
          导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['web:data:query:sdkDqDsTbCl:export']"
        >
          导出
        </el-button>
      </el-col> -->
    <!-- <right-toolbar v-model:showSearch="showSearch" @query-table="getList" />
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
      style="width: 99.9%"
      max-height="760px"
      :table-layout="auto"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="目录id" align="center" prop="cateId" />
      <el-table-column label="数据源id" align="center" prop="dsId" />
      <el-table-column label="数据表id" align="center" prop="tbId" /> -->
      <!-- <el-table-column label="所属单位" align="center" prop="tenantId" /> -->
      <el-table-column label="字段名称" align="center" prop="name" />
      <el-table-column label="字段注释" align="center" prop="comment" />
      <el-table-column label="序列名" align="center" prop="seqName">
        <template #default="{ row }">
          <div v-if="row.pkType === '序列'">
            <el-input v-model="row.seqName" placeholder="请输入列序名" />
          </div>
        </template>
      </el-table-column>

      <el-table-column
        label="主键"
        align="center"
        prop="pk"
        width="50"
        :formatter="(row) => (row.pk ? '是' : '否')"
      />
      <!-- <el-table-column label="主键类型" align="center" prop="pkType" /> -->
      <el-table-column
        label="允许null"
        align="center"
        prop="ableBeNull"
        width="80"
        :formatter="(row) => (row.ableBeNull ? '允许' : '不允许')"
      />
      <el-table-column label="默认展示" align="center" prop="defaultShow" width="140px">
        <template #default="{ row }">
          <el-switch
            size="small"
            v-model="row.defaultShow"
            :active-value="true"
            :inactive-value="false"
            active-text="展示"
            inactive-text="不展示"
            @change="handleShowChange(row, 'defaultShow')"
          />
        </template>
      </el-table-column>
      <el-table-column label="脱敏器" align="center" prop="desensitizer" width="150px">
        <template #default="{ row }">
          <el-select v-model="row.desensitizer" placeholder="请选择">
            <el-option
              v-for="item in sensitive_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        label="字段类型"
        align="center"
        prop="type"
        width="80px"
        :formatter="formatFieldType"
      />
      <!-- <el-table-column label="长度" align="center" prop="length" /> -->
      <!-- <el-table-column label="小数" align="center" prop="decimalLength" /> -->
      <!-- <el-table-column label="默认值" align="center" prop="defaultValue" /> -->
      <!-- <el-table-column label="表单java类型" align="center" prop="formJavaType" /> -->
      <el-table-column label="填报类型" align="center" prop="inputType" width="150px">
        <template #default="{ row }">
          <el-select v-model="row.inputType" placeholder="请选择">
            <el-option
              v-for="item in controlOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="字典类型" align="center" prop="dictType" width="150px">
        <template #default="{ row }">
          <div v-if="['radio', 'checkbox', 'select'].includes(row.inputType)">
            <el-select v-model="row.dictType" placeholder="请选择">
              <el-option
                v-for="item in typeList"
                :key="item.dictType"
                :label="item.dictName"
                :value="item.dictType"
              />
            </el-select>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="排序字段" align="center" prop="sortValue" /> -->
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="190"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            type="default"
            class="h-7"
            @click="handleSort(scope.row, scope.$index, 'top')"
            v-hasPermi="['web:data:query:sdkDqDsTbCl:edit']"
          >
            置顶
          </el-button>
          <el-button
            type="default"
            icon="ArrowUp"
            class="h-7"
            :disabled="scope.$index === 0"
            @click="handleSort(scope.row, scope.$index, 'pre')"
            v-hasPermi="['web:data:query:sdkDqDsTbCl:edit']"
          />
          <el-button
            type="default"
            class="h-7"
            icon="ArrowDown"
            :disabled="scope.$index === list.length - 1"
            @click="handleSort(scope.row, scope.$index, 'next')"
            v-hasPermi="['web:data:query:sdkDqDsTbCl:edit']"
          />
          <el-button
            plain
            class="h-7 mt-1"
            style="margin-left: 0"
            type="default"
            icon="View"
            @click="handleDetail(scope.row)"
            v-hasPermi="['web:data:query:sdkDqDsTbCl:info']"
          />
          <el-button
            plain
            class="h-7 mt-1"
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['web:data:query:sdkDqDsTbCl:edit']"
          />
          <el-button
            v-if="list.length > 1"
            plain
            class="h-7 mt-1"
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['web:data:query:sdkDqDsTbCl:remove']"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="pageParams.pageNum"
      v-model:limit="pageParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="title"
      v-model="open"
      width="1000px"
      append-to-body
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="formRef"
        :model="form"
        class="grid grid-cols-2 gap-3"
        :rules="rules"
        :disabled="!isEdit"
        label-width="96px"
      >
        <!-- <el-form-item label="目录id" prop="cateId">
          <el-input v-model="form.cateId" placeholder="请输入目录id" />
        </el-form-item> 
        <el-form-item label="数据源id" prop="dsId">
          <el-input v-model="form.dsId" placeholder="请输入数据源id" />
        </el-form-item>
        <el-form-item label="数据表id" prop="tbId">
          <el-input v-model="form.tbId" placeholder="请输入数据表id" />
        </el-form-item>
        <el-form-item label="所属单位" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入所属单位" />
        </el-form-item> -->
        <el-form-item label="数据列名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入数据列名称" />
        </el-form-item>
        <el-form-item label="数据列注释" prop="comment">
          <el-input v-model="form.comment" placeholder="请输入数据列注释" />
        </el-form-item>

        <el-form-item label="是否主键" prop="pk">
          <el-select v-model="form.pk" placeholder="请选择">
            <el-option label="是" :value="true" />
            <el-option label="否" :value="false" />
          </el-select>
        </el-form-item>

        <el-form-item label="主键类型" prop="pkType">
          <!-- <el-select v-model="form.pkType" placeholder="请选择主键类型">
            <el-option label="请选择字典生成" value="" />
          </el-select> -->
          <el-select
            v-model="form.pkType"
            placeholder="请选择主键类型"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="item in idStrategyOptions"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.pkType === '序列'" label="序列名称" prop="seqName">
          <el-input
            v-model="form.seqName"
            placeholder="请输入序列名称"
            @input="(v) => handleFormItemUpperCase(v, 'seqName')"
          />
        </el-form-item>
        <el-form-item label="允许null" prop="ableBeNull">
          <el-select v-model="form.ableBeNull" placeholder="请选择">
            <el-option label="是" :value="true" />
            <el-option label="否" :value="false" />
          </el-select>
        </el-form-item>
        <el-form-item label="默认展示" prop="defaultShow">
          <el-select v-model="form.defaultShow" placeholder="请选择">
            <el-option label="是" :value="true" />
            <el-option label="否" :value="false" />
          </el-select>
        </el-form-item>
        <el-form-item label="脱敏器" prop="desensitizer">
          <!-- <el-input v-model="form.desensitizer" placeholder="请输入脱敏器" /> -->
          <el-select v-model="form.desensitizer" placeholder="请选择">
            <el-option
              v-for="item in sensitive_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="字段类型" prop="type">
          <el-select v-model="form.type">
            <el-option
              v-for="(item, index) in fieldOptions"
              :key="index"
              :label="item.label"
              :value="item.jdbcType"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="默认值" prop="defaultValue">
          <el-input v-model="form.defaultValue" placeholder="请输入默认值" />
        </el-form-item>
        <el-form-item label="表单java类型" prop="formJavaType" label-width="96px">
          <el-select v-model="form.formJavaType" placeholder="请选择表单java类型">
            <el-option
              v-for="(item, index) in fieldJavaOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="长度" prop="length">
          <el-input-number
            style="width: 100%"
            :precision="0"
            v-model="form.length"
            placeholder="请输入长度"
          />
        </el-form-item>

        <el-form-item label="小数" prop="decimalLength">
          <el-input v-model="form.decimalLength" placeholder="请输入小数" />
        </el-form-item>
        <el-form-item label="填报类型" prop="inputType">
          <el-select
            v-model="form.inputType"
            placeholder="请选择"
            @change="(val) => handleInputChange(form)"
          >
            <el-option
              v-for="item in controlOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-select
            :disabled="!['radio', 'checkbox', 'select'].includes(form.inputType)"
            v-model="form.dictType"
            placeholder="请选择"
          >
            <el-option
              v-for="item in typeList"
              :key="item.dictType"
              :label="item.dictName"
              :value="item.dictType"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排序字段" prop="sortValue">
          <el-input-number
            style="width: 100%"
            v-model="form.sortValue"
            placeholder="请输入排序字段"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      :title="upload.title"
      v-model="upload.open"
      width="400px"
      append-to-body
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <el-upload
        ref="uploadRef"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <el-icon class="el-icon--upload">
          <upload-filled />
        </el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <div class="el-upload__tip">
              <el-checkbox v-model="upload.updateSupport" />
              是否更新已经存在的数据查询-数据表字段表数据
            </div>
            <span>仅允许导入xls、xlsx格式文件。</span>
            <el-link
              type="primary"
              :underline="false"
              style="font-size: 12px; vertical-align: baseline"
              @click="importTemplate"
              >下载模板
            </el-link>
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
  import { ref, watch } from 'vue';
  import { useOptions } from '@/views/web/bu/form/useConstant';

  import { listType } from '@/api/system/dict/type';
  // import { pageSdkDqChildCate } from '@/api/web/db/dataquery/sdkDqCate';
  import {
    updateBatchSdkDqDsTbCl,
    listSdkDqDsTbCl,
    // pageSdkDqDsTbCl,
    getSdkDqDsTbClInfo,
    // addSdkDqDsTbCl,
    // updateSdkDqDsTbCl,
    // delSdkDqDsTbCl,
    tplSdkDqDsTbCl,
    // exportSdkDqDsTbCl,
  } from '@/api/web/db/dataquery/sdkDqDsTbCl';
  import { getToken } from '@/utils/auth';
  // import { parseTime, addFieldTimeRange, addFieldDateRange } from '@/utils/base';
  import { useDict } from '@/utils/dict';
  import modal from '@/plugins/modal';
  import { useFieldOptions } from './useConstant';

  const { fieldOptions, fieldJavaOptions, formatFieldType } = useFieldOptions();

  const { controlOptions, idStrategyOptions } = useOptions();
  const { sensitive_type } = useDict('sensitive_type');

  // const fieldOptions = ref([]);
  const props = defineProps({
    selectPNode: {
      type: Object,
      default: () => ({}),
    },
  });

  const pageParams = ref({
    pageNum: 1,
    pageSize: 10,
  });
  const queryParams = ref({
    cateId: null,
    dsId: null,
    tbId: null,
    tenantId: null,
    name: null,
    comment: null,
    pk: null,
    pkType: null,
    ableBeNull: null,
    defaultShow: null,
    desensitizer: null,
    type: null,
    length: null,
    decimalLength: null,
    defaultValue: null,
    formJavaType: null,
    inputType: null,
    dictType: null,
    sortValue: null,
  });
  // const queryRef = ref(null);
  const list = ref([]);
  const open = ref(false);
  const loading = ref(false);
  const isEdit = ref(true);
  // const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref('');

  const uploadRef = ref(null);
  const upload = ref({
    open: false,
    title: '',
    isUploading: false,
    updateSupport: 0,
    headers: { Authorization: 'Bearer ' + getToken() },
    url: import.meta.env.VITE_APP_BASE_API + '/web/data/query/sdkDqDsTbCl/import',
  });
  const queryTypeParams = ref({ status: '0' });
  const typeList = ref([]);

  const formRef = ref(null);
  const form = ref({});
  const rules = ref({
    name: [
      { required: true, message: '数据列名必填' },
      {
        min: 0,
        max: 64,
        message: '数据列名称长度必须介于 0 和 64 之间',
        trigger: 'blur',
      },
    ],
    comment: [
      // { required: true, message: '列注释名必填' },
      {
        min: 0,
        max: 64,
        message: '数据列注释长度必须介于 0 和 64 之间',
        trigger: 'blur',
      },
    ],

    pkType: [
      {
        min: 0,
        max: 64,
        message: '主键类型长度必须介于 0 和 64 之间',
        trigger: 'change',
      },
    ],

    desensitizer: [
      {
        min: 0,
        max: 64,
        message: '脱敏器长度必须介于 0 和 64 之间',
        trigger: 'blur',
      },
    ],
    type: [
      {
        min: 0,
        max: 64,
        message: '字段类型长度必须介于 0 和 64 之间',
        trigger: 'change',
      },
    ],
    // length: [{}],
    // decimalLength: [{}],
    // defaultValue: [{}],
    formJavaType: [
      {
        min: 0,
        max: 64,
        message: '表单java类型长度必须介于 0 和 64 之间',
        trigger: 'change',
      },
    ],
    inputType: [
      {
        min: 0,
        max: 64,
        message: '填报类型长度必须介于 0 和 64 之间',
        trigger: 'change',
      },
    ],
    dictType: [
      {
        min: 0,
        max: 64,
        message: '字典类型长度必须介于 0 和 64 之间',
        trigger: 'change',
      },
    ],
  });
  // const cateInfo = ref({});

  watch(
    () => props.selectPNode,
    (pnode) => {
      if (pnode) {
        queryParams.value.tbId = pnode.id;

        getList();
        // getCateInfo();
      }
    },
    { immediate: true },
  );
  function getList() {
    loading.value = true;
    listSdkDqDsTbCl(queryParams.value)
      .then((res) => {
        console.log({ llll: res });
        list.value = res?.data ?? [];
        // total.value = res?.data?.total ?? 0;
      })
      .finally(() => {
        loading.value = false;
      });
  }

  /** 查询字典类型列表 */
  function getTypeList() {
    // loading.value = true;
    listType(queryTypeParams.value, { pageNum: 1, pageSize: 99999 }).then((res) => {
      // loading.value = false;
      if (res.data) {
        typeList.value = res.data.records ? res.data.records : [];
      } else {
        typeList.value = [];
      }
      console.log({ tyl: typeList.value });
    });
  }

  // async function getFormColTypeOption() {
  //   const res = await getFormColType();
  //   console.log({ res });
  //   fieldOptions.value = res.data;
  // }

  function handleSave() {
    updateBatchSdkDqDsTbCl(queryParams.value.tbId, list.value).then((res) => {
      console.log({ res22: res });
      if (res.code === 200) {
        modal.msgSuccess('保存成功');
        getList();
      }
    });
  }

  function handleInputChange(info) {
    info.dictType = null;
  }
  function handleFormItemUpperCase(v, field) {
    form.value[field] = (v?.replace(/[^a-zA-Z_]/g, '') || '').toUpperCase();
  }

  function handleShowChange() {
    // console.log({ row, prop });
    // let text = row[prop] ? '展示' : '隐藏';
    // modal
    //   .confirm('确认要"' + text + '""' + row.comment + '"列吗?')
    //   .then(function () {
    //     return updateSdkDqDsTbCl(row);
    //   })
    //   .then(() => {
    //     modal.msgSuccess(text + '成功');
    //   })
    //   .catch(function () {
    //     row[prop] = !row[prop];
    //   });
  }
  function cancel() {
    open.value = false;
    reset();
  }
  // 获取cateId
  // function getCateInfo() {
  //   pageSdkDqChildCate({ id: props.selectPNode.pid }).then((res) => {
  //     console.log({ cateinfo: res, pid: props.selectPNode.pid, selectPNode: props.selectPNode });
  //   });
  // }
  function reset() {
    let cateId = null;
    if (list.value.length) {
      cateId = list.value[0].cateId;
    }
    form.value = {
      id: null,
      cateId: cateId,
      dsId: props.selectPNode.pid,
      tbId: props.selectPNode.id,
      tenantId: null,
      name: null,
      comment: null,
      pk: false,
      pkType: null,
      ableBeNull: true,
      defaultShow: true,
      desensitizer: null,
      type: null,
      length: null,
      decimalLength: null,
      defaultValue: null,
      formJavaType: null,
      inputType: null,
      dictType: null,
      sortValue: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null,
      seqName: null,
    };
    formRef.value?.resetFields();
  }

  // function handleQuery() {
  //   pageParams.value.pageNum = 1;
  //   getList();
  // }

  // function resetQuery() {
  //   queryRef.value?.resetFields();
  //   handleQuery();
  // }

  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.id);
    single.value = selection.length !== 1;
    multiple.value = !selection.length;
  }

  function handleAdd() {
    reset();
    title.value = '添加数据查询-数据表字段表';

    isEdit.value = true;
    open.value = true;
  }
  function handleDetail(row) {
    reset();
    const _id = row?.id ?? ids.value[0];
    getSdkDqDsTbClInfo(_id).then((res) => {
      form.value = res.data;
      isEdit.value = false;
      title.value = '查看数据查询-数据表字段表';
      open.value = true;
    });
  }
  function handleUpdate(row) {
    reset();
    const _id = row?.id ?? ids.value[0];
    getSdkDqDsTbClInfo(_id).then((res) => {
      form.value = res.data;
      isEdit.value = true;
      title.value = '修改数据查询-数据表字段表';
      open.value = true;
    });
  }
  function handleSort(row, index, type) {
    const preItm = list.value[index - 1];
    const nextItm = list.value[index + 1];
    const nowSValue = row.sortValue;

    switch (type) {
      case 'top':
        for (let i = index; i >= 0; i--) {
          const newSValue = list.value[i].sortValue;
          if (i === 0) {
            list.value[i] = { ...row, sortValue: newSValue };
          } else {
            list.value[i] = { ...list.value[i - 1], sortValue: newSValue };
          }
          // console.log({ list: list.value });
        }

        break;
      case 'pre':
        const preSValue = preItm.sortValue;
        list.value[index - 1] = { ...row, sortValue: preSValue };
        list.value[index] = { ...preItm, sortValue: nowSValue };
        break;
      case 'next':
        const nextSValue = nextItm.sortValue;
        list.value[index] = { ...nextItm, sortValue: nowSValue };
        list.value[index + 1] = { ...row, sortValue: nextSValue };
        break;
      default:
        break;
    }
  }

  function submitForm() {
    formRef.value.validate((valid) => {
      if (valid) {
        // if (form.value.id) {
        //   updateSdkDqDsTbCl(form.value).then(() => {
        //     modal.msgSuccess('修改成功');
        //     open.value = false;
        //     getList();
        //   });
        // } else {
        //   addSdkDqDsTbCl(form.value).then(() => {
        //     modal.msgSuccess('新增成功');
        //     open.value = false;
        //     getList();
        //   });
        // }
        let data = [...list.value];
        if (!form.value.id) {
          data.push(form.value);
        } else {
          const fIndex = data.findIndex((item) => item.id === form.value.id);
          data.splice(fIndex, 1, form.value);
        }
        updateBatchSdkDqDsTbCl(queryParams.value.tbId, data).then((res) => {
          // console.log({ res22: res });
          if (res.code === 200) {
            modal.msgSuccess('保存成功');
            open.value = false;
            getList();
          }
        });
      }
    });
  }

  function handleDelete(row) {
    const _ids = row?.id; // ?? ids.value.join(',');
    modal
      .confirm('是否确认删除数据查询-数据表字段表编号为"' + _ids + '"的数据项？')
      .then(() => {
        const data = list.value.filter((item) => item.id === _ids);
        return updateBatchSdkDqDsTbCl(queryParams.value.tbId, data);
      })
      .then(() => {
        getList();
        modal.msgSuccess('删除成功');
      })
      .catch(() => {});
  }

  // function handleExport() {
  //   exportSdkDqDsTbCl(pageParams.value, queryParams.value);
  // }

  // function handleImport() {
  //   upload.value.title = '数据查询-数据表字段表导入';
  //   upload.value.open = true;
  // }

  function importTemplate() {
    tplSdkDqDsTbCl();
  }

  function handleFileUploadProgress() {
    upload.value.isUploading = true;
  }

  function handleFileSuccess(res, file) {
    upload.value.open = false;
    upload.value.isUploading = false;
    uploadRef.value.handleRemove(file);
    modal.msgSuccess(res.msg);
    getList();
  }

  function submitFileForm() {
    uploadRef.value.submit();
  }

  // getList();

  getTypeList();
  // getFormColTypeOption();
</script>
